package com.viewplug.listView;


import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.viewplug.R;

/**
 * Created by mac_wk on 14-10-27.
 */
public class ListViewHeader extends LinearLayout {
    private final int ROTATE_ANIM_DURATION = 180;

    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_REFRESHING = 2;
    private LinearLayout mContainer;
    private ImageView mArrowImageView;
    private ProgressBar mProgressBar;
    private TextView mHintTextView;
    private int mState = STATE_NORMAL;
    private TextView viewplug_listview_header_hint_textview;
    private Animation mRotateUpAnim;
    private Animation mRotateDownAnim;
    private void initView(Context context) {

        //默认高度为0
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,0);
        mContainer = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.view_plug_listview_header,null);
        addView(mContainer,lp);
        //设置位于底部
        setGravity(Gravity.BOTTOM);

        mArrowImageView = (ImageView) findViewById(R.id.viewplug_listview_header_arrow);
        mHintTextView = (TextView) findViewById(R.id.viewplug_listview_header_hint_textview);
        mProgressBar = (ProgressBar) findViewById(R.id.viewplug_listview_header_progressbar);

        mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateUpAnim.setFillAfter(true);
        mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateDownAnim.setFillAfter(true);
    }
    public void setState(int state)
    {
        if (state==mState)return ;
        if (state==STATE_REFRESHING)//正在刷新中 显示进度
        {
            mArrowImageView.clearAnimation();
            mArrowImageView.setVisibility(View.INVISIBLE);
            mProgressBar.setVisibility(View.VISIBLE);
        }else// 显示箭头图片
        {
            mArrowImageView.setVisibility(View.VISIBLE);
            mProgressBar.setVisibility(View.INVISIBLE);
        }
        switch (state)
        {
            case STATE_NORMAL:
                if (mState==STATE_READY)
                {
                    mArrowImageView.setAnimation(mRotateDownAnim);
                }
                if (mState==STATE_REFRESHING)
                {
                    mArrowImageView.clearAnimation();
                }
                mHintTextView.setText(R.string.view_plug_listview_drug_txt);
                break;
            case STATE_READY:
                if (mState !=STATE_READY)
                {
                    mArrowImageView.clearAnimation();
                    mArrowImageView.startAnimation(mRotateUpAnim);
                    mHintTextView.setText(R.string.view_plug_listview_drug_txt_ready);
                }
                break;
            case STATE_REFRESHING:
                mHintTextView.setText(R.string.view_plug_listview_drug_loading);
                break;
        }
        mState = state;
    }
    public void setVisiableHeight(int height)
    {
        if (height<0)
            height=0;
        ViewGroup.LayoutParams lp =  mContainer.getLayoutParams();

        lp.height = height;
        mContainer.setLayoutParams(lp);

    }
    public int getVisibleHeight (){return mContainer.getHeight();}
    public ListViewHeader(Context context) {
        super(context);
        initView(context);
    }



    public ListViewHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public ListViewHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @TargetApi(Build.VERSION_CODES.L)
    public ListViewHeader(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }


}
